package com.xonro.common.histrans.sfeg.biz.cm;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.util.UUIDGener;
import com.actionsoft.emm.util.DateUtil;
import com.actionsoft.sdk.local.SDK;
import com.xonro.common.histrans.base.ctx.BOCreateData;
import com.xonro.common.histrans.base.ctx.FileTransData;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.base.model.HistoryTransLogModel;
import com.xonro.common.histrans.base.consts.SfegConst;
import com.xonro.common.histrans.sfeg.biz.CommTransBiz;
import com.xonro.common.histrans.sfeg.biz.common.SfegProcessTransBiz;
import com.xonro.common.histrans.sfeg.ctx.SfecProcessCreateContext;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

public class BorrowApplyBiz  extends SfegProcessTransBiz {
    @Override
    protected void transProcess0(SfecProcessCreateContext historyTransContext) {
        String createUser = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getCreateUser();
        String bindId = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getId();
        Timestamp startTime = historyTransContext.getProcessCreateContext().getProcessInstanceModel().getStartTime();
        List<BOCreateData> boCreateDataList = new ArrayList<>();
        RowMap mainData = historyTransContext.getMainData();

        //添加日志
        String mainBOId = UUIDGener.getUUID();
        HistoryTransLogModel logModel = new HistoryTransLogModel();
        historyTransContext.setHistoryTransLogModel(logModel);
        logModel.setBindId(bindId);
        logModel.setBpmBOId(mainBOId);
        logModel.setBpmTable("BO_XR_EFM_BORROW_APPLY");//借款单
        logModel.setOuterTable("tb_CompanyLoanBill");//借款单
        logModel.setOuterPK(mainData.getString("id"));
        logModel.setOuterExt1(mainData.getString("ctCode"));
        logModel.setOuterExt2(mainData.getString("HTBH"));
        logModel.setOuterExt3(mainData.getString("ctType"));//合同类型
        BOCreateData mainBOCreateData = new BOCreateData();
        RowMap loanData = SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMap("SELECT * FROM tb_CompanyLoanBill WHERE lbCode = ?",mainData.getString("applyCode"));
        RowMap detailData = SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMap("SELECT * FROM tb_CompanyLoanBillDetail WHERE lbCode = ?",mainData.getString("applyCode"));
        boCreateDataList.add(mainBOCreateData);
        BO mainBO = new BO();
        mainBOCreateData.setBo(mainBO);
        mainBOCreateData.setCreateUser(createUser);
        mainBO.setCreateDate(startTime);
        mainBO.setUpdateDate(startTime);
        mainBO.setId(mainBOId);
        mainBO.set("APPLY_DATE", DateUtil.parse( mainData.getString("applyDate"), "yyyy-MM-dd"));
        mainBO.set("APPLY_NO",  mainData.getString("applyCode"));//申请单号
        mainBO.set("EXPENSE_TYPE_CODE", mainData.getString("feeType"));//费用类型编码
        mainBO.set("EXPENSE_TYPE_CODE", mainData.getString("feeType"));//费用类型编码
        mainBO.set("AMOUNT", mainData.getString("applyTotal"));//借款金额
        mainBO.set("OPERATION_DEPT", mainData.getString("applyDept"));//部门ID
        mainBO.set("OPERATOR", CommTransBiz.getInstance().getUserId( mainData.getString("applyUser")));//申请人
        mainBO.set("USER_ID", CommTransBiz.getInstance().getUserId( mainData.getString("applyUser")));//申请人
        mainBO.set("ACTUAL_PAY_AMOUNT", mainData.getDate("realTotal"));//实际金额
        mainBO.set("PAY_TYPE", mainData.getString("paytype"));//支付方式
        mainBO.set("AMOUNT_CAPITAL",loanData.getString( "loanTotalCapital"));//金额大写
        mainBO.set("REASON", loanData.getString("lbReason"));//借款用途
        if(null!=detailData) {
            mainBO.set("REPAID_AMOUNT", detailData.get("total"));//还款金额
            mainBO.set("WRITE_OFF_AMOUNT", detailData.get("total"));// 已冲销金额
        }
        mainBO.set("PROJECT_NO", mainData.getString("projid"));
        historyTransContext.getProcessCreateContext().setBoCreateDatas(boCreateDataList);


        //添加附件
        FileTransData fileTransData = new FileTransData();
        fileTransData.setBoId(mainBOId);
        //OA表订单编号
        fileTransData.setFkCode(mainData.getString("applyCode"));
        fileTransData.setProcessInstId(bindId);
        historyTransContext.getFileTransDatas().add(fileTransData);
        mainBOCreateData.setBoName("BO_XR_EFM_BORROW_APPLY");
        fileTransData.setBoName("BO_XR_EFM_BORROW_APPLY");
        fileTransData.setFieldName("ATTACHMENT");


    }


    @Override
    protected String getProcessDefId() {
        return "obj_94acb51daa2c412c8a84a4305f0cb291";//借款申请流程定义ID
    }

    @Override
    protected String getActivityDefId() {
        return "obj_6bf437e898654718a19abc20deeff04d";//借款申请第一节点ID
    }

    @Override
    protected String getTitle(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        String kxName =mainData.getString("itemName");//款项名称
        String jkCode = mainData.getString("applyCode");//借款单号

        return kxName+jkCode;
    }

    @Override
    protected String getEnCode(HistoryTransContext ctx) {
        RowMap mainData = ctx.getMainData();
        return mainData.getString("applyCode");
    }

    @Override
    protected String getOuterTable(SfecProcessCreateContext historyTransContext) {
        return "tb_MoneyInOutAll";
    }

    @Override
    protected String getPk(SfecProcessCreateContext historyTransContext) {
        RowMap mainData = historyTransContext.getMainData();
        return mainData.getString("id");
    }

    @Override
    protected List<RowMap> getMainDatas() {
        //查找借款单数据
        return SDK.getCCAPI().getRDSAPI(SfegConst.CCID).getMaps("SELECT * FROM tb_MoneyInOutAll  WHERE applycorp = '1040' and feeType='f0236' ");
    }

    @Override
    protected RowMap getMainData() {
        return null;
    }
}
